package servlet.news;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jdbc.DBUtils;
import servlet.UtfUtil;

@WebServlet("/NewsAddServlet")
public class NewsAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Connection connection = DBUtils.getConn();
		
		try {
			String channelId = request.getParameter("channel_id");
		 	 channelId = UtfUtil.Utf(channelId);
			 String title = request.getParameter("title");
			 title = new String(title.getBytes("iso-8859-1"),"utf-8");
			 String content = request.getParameter("content");
			 content = new String(content.getBytes("iso-8859-1"),"utf-8");
			 String linktype = request.getParameter("linktype");
			 linktype = new String(linktype.getBytes("iso-8859-1"),"utf-8");
			 String linkurl = request.getParameter("linkurl");
			 linkurl = new String(linkurl.getBytes("iso-8859-1"),"utf-8");
			PreparedStatement ps = connection.prepareStatement("INSERT into news(title,content,linktype,linkurl,channel_id) VALUES(?,?,?,?,?)");
			ps.setString(1,title);
			ps.setString(2,content);
			ps.setInt(3,Integer.valueOf(linktype));
			ps.setString(4,linkurl);
			ps.setInt(5,Integer.valueOf(channelId));
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//1、强制改变 url 让其重新请求   send：发送 redirect： 重定向 
		response.sendRedirect("NewsListServlet");
		//2、把请求转发给 另一个服务器 url 不变  Dispatcher： 调度员  forward 前进
		//request.getRequestDispatcher("NewsListServlet").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}


